﻿Namespace Utils
    Public NotInheritable Class Util
        ''' <summary>
        ''' Get a Control in a collection of Controls (recursively)
        ''' </summary>
        ''' <param name="name">Name of the control to find</param>
        ''' <param name="rootControl">Root control where to look for</param>
        ''' <returns></returns>
        ''' <remarks>Be carefull it will return the first control with that name</remarks>
        Public Shared Function GetControlByName(ByVal name As String, ByVal rootControl As Control) As Control
            '
            If rootControl.Name = name Then
                Return rootControl
            End If
            '
            For Each Ctl As Control In rootControl.Controls
                Dim FoundCtl As Control = GetControlByName(name, Ctl)
                If FoundCtl IsNot Nothing Then
                    Return FoundCtl
                End If
            Next
            '
            Return Nothing
        End Function



        ''' <summary>
        ''' Get the DefaultText from MetaString base on the Current Language
        ''' </summary>
        ''' <param name="code"></param>
        ''' <returns>Text from the Metastring, if not found String.Empty</returns>
        ''' <remarks>Get the DefaultText from MetaString base on the Current Language. 
        ''' Language is set in Option as: Language. do not confuse with PRM or ALT display column</remarks>
        Public Shared Function GetMessage(ByVal code As String) As String
            Try
                Dim ms As MetaString = MetaString.GetMetaStringByCode(code, Base.AppManager.ApplicationOption.LanguageId)
                If ms Is Nothing Then
                    Return String.Format("MetaString [{0}] not defined / Non définie", code)
                End If
                Return ms.DefaultText
            Catch ex As Exception
                Return String.Format("MetaString [{0}] Please connect to a Database / SVP connecter vous a une base de donnée", code)
            End Try
        End Function

    End Class

    ''' <summary>
    ''' Class pour identidfier a partir de la datarow les champs que nous avons pour la Meta IMExplorer
    ''' </summary>
    ''' <remarks>le design de cela consite en : 
    ''' pour aider dans le navigation du tree view.
    '''</remarks>
    Public Class ExplorerNodeInfo
        Implements IDisposable

        Public Property NodeID As Guid
        Public Property ParentID As Guid
        Public Property NodeStatus As Guid
        Public Property QueryName As String
        Public Property PL As String
        Public Property NodeDescription_PRM As String
        Public Property NodeDescription_ALT As String
        Public Property NodeActivated As Boolean
        Public Property NodeSectionRev As Decimal 'keep



        Public Property NodeDataRow As DataRow

        Public Sub New()
        End Sub

        Public Sub New(ByVal datarow As DataRow)
            Assign(datarow)
        End Sub

        Public Overridable Sub Assign(ByVal datarow As DataRow)
            NodeDataRow = datarow

            If Not IsDBNull(NodeDataRow.Item("EntityID")) Then
                NodeID = CType(datarow("EntityID"), Guid)
            End If

            If Not IsDBNull(NodeDataRow.Item("ParentID")) Then
                If Not Guid.TryParse(datarow("ParentID").ToString, ParentID) Then
                    ParentID = Guid.Empty
                End If
            End If

            If Not IsDBNull(NodeDataRow.Item("MetaQuerie")) Then
                QueryName = CType(datarow("MetaQuerie"), String)
            End If

            If Not IsDBNull(NodeDataRow.Item("PL")) Then
                PL = CType(datarow("PL"), String)
            End If

            If Not IsDBNull(NodeDataRow.Item("Activated")) Then
                NodeActivated = CBool(datarow("Activated"))
            End If

            If Not IsDBNull(NodeDataRow.Item("EntityRevision")) Then
                NodeSectionRev = CDec(datarow("EntityRevision"))
            End If

            If Not IsDBNull(NodeDataRow.Item("EntityStatusID")) Then
                NodeStatus = CType(datarow("EntityStatusID"), Guid)
            End If

            If Not IsDBNull(NodeDataRow.Item("Description_PRM")) Then
                NodeDescription_PRM = CType(datarow("Description_PRM"), String)
            End If

            If Not IsDBNull(NodeDataRow.Item("Description_ALT")) Then
                NodeDescription_ALT = CType(datarow("Description_PRM"), String)
            End If





        End Sub

#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                End If

                ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

        ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
        'Protected Overrides Sub Finalize()
        '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class


End Namespace
